
import UIKit
import GRDB
import web3swift
import BigInt




struct DateNoticeModel: Codable {
    
    var noticeId = ""
    var text = ""
    var year = ""
    var month = ""
    var day = ""
    var hour = ""
    var minute = ""
    var second = ""
    
    
    
    private enum Columns: String, CodingKey, ColumnExpression {
        
        case noticeId
        case text
        case year
        case month
        case day
        case hour
        case minute
        case second
    }
}


extension DateNoticeModel: MutablePersistableRecord, FetchableRecord {
    
    // 数据库对象
    private static let dbQueue: DatabaseQueue = DataManager.dbQueue
    
    // MARK: 创建表
    private static func creatTable() -> Void {
        
        try! self.dbQueue.write({ (db) -> Void in
            
            if try db.tableExists("DateNoticeModel") {
                // 已经存在
                return
            }
            
            try db.create(table: "DateNoticeModel", body: { (t) in
                
                t.column(Columns.noticeId.rawValue, Database.ColumnType.text)
                t.column(Columns.text.rawValue, Database.ColumnType.text)
                t.column(Columns.year.rawValue, Database.ColumnType.text)
                t.column(Columns.month.rawValue, Database.ColumnType.text)
                t.column(Columns.day.rawValue, Database.ColumnType.text)
                t.column(Columns.hour.rawValue, Database.ColumnType.text)
                t.column(Columns.minute.rawValue, Database.ColumnType.text)
                t.column(Columns.second.rawValue, Database.ColumnType.text)
                
            })
            
        })
    }
    
    // MARK: 插入单个
    static func addData(_ model: DateNoticeModel) -> Void {
        
        // 已存在，更新
        guard self.getData(model.noticeId) == nil else {
            self.updateData(model)
            return
        }
        
        self.creatTable()
        try! self.dbQueue.write({ (db) -> Void in
            // 插入数据库
            var testModel = model
            try testModel.insert(db)
        })
    }
    
    // MARK: 查询单个【根据noticeId】
    static func getData(_ noticeId: String) -> DateNoticeModel? {
        
        self.creatTable()
        return try! self.dbQueue.unsafeRead({ (db) -> DateNoticeModel? in
            return try DateNoticeModel.filter(Column(Columns.noticeId.rawValue) == noticeId).fetchOne(db)
        })
    }
    
    
    // MARK: 查询所有【根据卡上架时间排序】
    static func getDatas() -> [DateNoticeModel] {
        
        var temp = self.selfAllDatas()
        temp = temp.sorted(by: { (model1, model2) -> Bool in
            return model1.noticeId.integerValue > model2.noticeId.integerValue
        })
        return temp
    }
    
    static func selfAllDatas() -> [DateNoticeModel] {
        
        self.creatTable()
        return try! self.dbQueue.unsafeRead({ (db) -> [DateNoticeModel] in
            return try self.fetchAll(db)
        })
    }
    
    // MARK: 更新
    static func updateData(_ model: DateNoticeModel) -> Void {
        
        self.creatTable()
        
        // 先删除，在插入
        self.deleteData(model.noticeId)
        self.addData(model)
    }
    
    // MARK: 根据ID删除
    static func deleteData(_ noticeId: String) {
        // 查询
        guard let model = self.getData(noticeId) else {
            return
        }
        // 删除
        self.creatTable()
        do {
            try self.dbQueue.write({ db in
                try db.execute(sql: "delete from DateNoticeModel where noticeId=:noticeId", arguments: ["noticeId" : model.noticeId])
            })
        } catch {
        }
    }
    
    // MARK: 删除所有
    static func deleteDatas() {
        self.creatTable()
        do {
            try self.dbQueue.write({ db in
                try db.execute(sql: "delete from DateNoticeModel")
            })
        } catch {
        }
    }
    
    // MARK: 随机一个商店ID
    static func getOneNoticeId() -> String {
        
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyyMMddHHmmss"
        let dateText = formatter.string(from: Date.init(timeIntervalSinceNow: 0))
        let walletId = String.init(format: "%@%ld", dateText, arc4random()%1000)
        return walletId
    }
}
